package com.centaline.androidsalesblog.activities.sale;

import android.content.Intent;
import android.view.View;

import com.android.volley.VolleyError;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.mine.FootPrintActivity;
import com.centaline.androidsalesblog.api.sale.PostListApi;
import com.centaline.androidsalesblog.base.BaseFragment;
import com.centaline.androidsalesblog.base.BasePostListMapActivity;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.fragments.list.SaleListFragment;
import com.centaline.androidsalesblog.fragments.listmap.PostListMapFragment;
import com.centaline.androidsalesblog.iml.RefreshListener;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.centaline.androidsalesblog.model.responseobject.PostListRo;
import com.centaline.androidsalesblog.rx.database.DropSaleRegionFunc1;
import com.centaline.androidsalesblog.rx.database.MetroFunc1;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.rx.database.SearchDataFunc1;
import com.centaline.androidsalesblog.widgets.dropdown.MoreDrop;
import com.centaline.androidsalesblog.widgets.dropdown.SalePriceDrop;
import com.centaline.androidsalesblog.widgets.dropdown.SingleDrop;
import com.centaline.androidsalesblog.widgets.dropdown.TrebleDrop;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.ArrayList;
import java.util.Locale;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func3;
import rx.functions.Func7;
import rx.schedulers.Schedulers;

/**
 * 地铁房
 * Created by guilin on 16/1/26.
 */
public class MetroListActivity extends BasePostListMapActivity {


    private TrebleDrop trebleDrop;
    private SalePriceDrop salePriceDrop;
    private SingleDrop singleDrop;
    private MoreDrop moreDrop;

    private SaleListFragment saleListFragment;
    private PostListMapFragment postListMapFragment;

    @Override
    protected void findViews() {
        super.findViews();
    }

    @Override
    protected void initApi() {
        postListApi = new PostListApi(SaleListActivity.class, PostListRo.class, new IResponseListener<PostListRo>() {
            @Override
            public void onResponse(PostListRo postListRo) {
                saleListFragment.stopRefresh(postListRo.getList());
                postListMapFragment.setMapArrayList(postListRo.getList(), postListApi.getStartIndex() == 0);
                if (postListApi.getStartIndex() == 0) {
                    snack(postListRo.getTotal() == 0 ?
                            getString(R.string.snack_post_nothing) :
                            String.format(Locale.CHINA, getString(R.string.snack_post), postListRo.getTotal()));
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                saleListFragment.refreshError();
                netWorkUnable();
            }
        });
        postListApi.setPostType("s");
        postListApi.setType("metro");
    }

    @Override
    protected void downRefresh(boolean needLocation, boolean clearLocation) {
        this.needLocation = needLocation;
        if (clearLocation) {
            saleListFragment.setLocation(null);
        }
        saleListFragment.startRefresh();
    }

    @Override
    protected BaseFragment getFragment1() {
        saleListFragment = new SaleListFragment();
        saleListFragment.setViewType(1);
        saleListFragment.setRefreshListener(new RefreshListener() {
            @Override
            public void down() {
                postListApi.setStartIndex(0);
                if (needLocation) {
                    startLocation();
                } else {
                    requestList();
                }
            }

            @Override
            public void up(int count) {
                postListApi.setStartIndex(count);
                requestList();
            }
        });
        return saleListFragment;
    }

    @Override
    protected BaseFragment getFragment2() {
        postListMapFragment = new PostListMapFragment();
        postListMapFragment.setBottomSheetStatus(bottomSheetStatus);
        return postListMapFragment;
    }

    @Override
    protected void popDismiss(boolean reset) {
        postListMapFragment.updateMapStatusByBottomSheet(reset);
    }

    @Override
    protected void initDrop() {
        dropDownView.initTabs(R.array.metro);
        trebleDrop = new TrebleDrop(dropDownView, this);
        salePriceDrop = new SalePriceDrop(dropDownView, this);
        singleDrop = new SingleDrop(dropDownView, this);
        moreDrop = new MoreDrop(dropDownView, this);
        dropDownView.addDrops(trebleDrop, salePriceDrop, singleDrop, moreDrop);

        Observable.combineLatest(RxDataBase.getMetro()
                        .subscribeOn(Schedulers.io())
                        .map(new MetroFunc1()),
                RxDataBase.nearby(),
                RxDataBase.getSaleRegions()
                        .subscribeOn(Schedulers.io())
                        .map(new DropSaleRegionFunc1()),
                new Func3<ArrayList<DropBo>, DropBo, ArrayList<DropBo>, ArrayList<DropBo>>() {
                    @Override
                    public ArrayList<DropBo> call(ArrayList<DropBo> dropBos, DropBo nearBy, ArrayList<DropBo> dropBos3) {
                        ArrayList<DropBo> list = new ArrayList<>();
                        DropBo dropBo;
                        if (dropBos.size() > 0) {
                            dropBo = new DropBo(20);
                            dropBo.setText("地铁");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos);
                            list.add(dropBo);
                        }
                        if (dropBos3.size() > 0) {
                            dropBo = new DropBo(10);
                            dropBo.setText("区域");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(new ArrayList<DropBo>());
                            dropBo.setChildrenList(new ArrayList<DropBo>());
                            DropBo dropBo1 = new DropBo(10);
                            dropBo1.setText(CommonConstant.DROP_TEXT_NO_LIMITED);
                            dropBo1.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.getChildrenList().add(dropBo1);
                            if (AppConstant.NearbyEnable) {
                                dropBo.getChildrenList().add(nearBy);
                            }
                            dropBo.getChildrenList().addAll(dropBos3);
                            list.add(dropBo);
                        }
                        return list;
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        trebleDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Sell")
                .map(new SearchDataFunc1(30))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        salePriceDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Room")
                .map(new SearchDataFunc1(40))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        singleDrop.init(dropBos);
                    }
                });
        Observable.combineLatest(
                RxDataBase.getSearchData("SellOrderby").map(new SearchDataFunc1(100)),
                RxDataBase.getSearchData("Direction").map(new SearchDataFunc1(50)),
                RxDataBase.getSearchData("Area").map(new SearchDataFunc1(60)),
                RxDataBase.getSearchData("Fitment").map(new SearchDataFunc1(70)),
                RxDataBase.getSearchData("SellTag").map(new SearchDataFunc1(80)),
                RxDataBase.getSearchData("Construction").map(new SearchDataFunc1(90)),
                RxDataBase.getSearchData("Property").map(new SearchDataFunc1(110)),
                new Func7<ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>,
                        ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>>() {
                    @Override
                    public ArrayList<DropBo> call(ArrayList<DropBo> dropBos, ArrayList<DropBo> dropBos2,
                                                  ArrayList<DropBo> dropBos3, ArrayList<DropBo> dropBos4,
                                                  ArrayList<DropBo> dropBos5, ArrayList<DropBo> dropBos6,
                                                  ArrayList<DropBo> dropBos7) {
                        final ArrayList<DropBo> list = new ArrayList<>();
                        DropBo dropBo;
                        if (dropBos.size() > 0) {
                            dropBo = new DropBo(100);
                            dropBo.setText("排序");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos);
                            list.add(dropBo);
                        }
                        if (dropBos2.size() > 0) {
                            dropBo = new DropBo(50);
                            dropBo.setText("朝向");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos2);
                            list.add(dropBo);
                        }

                        if (dropBos3.size() > 0) {
                            dropBo = new DropBo(60);
                            dropBo.setText("面积");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos3);
                            list.add(dropBo);
                        }
                        if (dropBos4.size() > 0) {
                            dropBo = new DropBo(70);
                            dropBo.setText("装修");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos4);
                            list.add(dropBo);
                        }
                        if (dropBos5.size() > 0) {
                            dropBo = new DropBo(80);
                            dropBo.setText("特色");
                            dropBo.setSelected(true);
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos5);
                            list.add(dropBo);
                        }
                        if (dropBos6.size() > 0) {
                            dropBo = new DropBo(90);
                            dropBo.setText("房龄");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos6);
                            list.add(dropBo);
                        }
                        if (dropBos7.size() > 0) {
                            dropBo = new DropBo(110);
                            dropBo.setText("类型");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos7);
                            list.add(dropBo);
                        }
                        return list;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        moreDrop.init(dropBos);
                    }
                });
    }

    @Override
    protected String getDefaultListType() {
        return "metro";
    }

    @Override
    protected CharSequence getSearchDefaultTitle() {
        return getString(R.string.title_metro);
    }

    @Override
    protected void requestList() {
        postListApi.startRequest();
    }

    @Override
    protected void locationInfo(BDLocation bdLocation) {
        switch (bdLocation.getLocType()) {
            case 61:
            case 161:
                LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                saleListFragment.setLocation(latLng);
                postListApi.setLatLng(latLng);
                requestList();
                break;
            default:
                saleListFragment.setLocation(null);
                saleListFragment.stopRefresh(null);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fab:
                startActivity(new Intent(this, FootPrintActivity.class));
                break;
            default:
                break;
        }
    }
}
